<?php
namespace app\admin\controller;
use think\Db;
class Sign extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['s_type'] = $role;
            }
            if(session ('area')){
                $map['s_qu'] = ['in',session('area')];
            }else{
                $map['s_qu'] = null;
            }
            if(isset($key)&&$key!="")
            {
                $map['s_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('sign_prize')
                ->where($map)
                ->count();
            $lists =  Db::name('sign_prize')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('s_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $status = Db::name('admin')->where('id',session('uid'))->value('sign');
        return $this->fetch('sign/index',['status'=>$status]);
    }

    public function signAdd(){
        if(request()->isPost ()){
            extract (input());
            $param = [
                's_day'=>$day,
                's_type'=>$type,
                's_num'=>$jifen,
                's_qu'=>session('myarea'),
                's_add_time'=>date('Y-m-d H:i:s'),
                's_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($type == 2){
                $name = Db::name('product')->where('p_id',$goods)->value('p_name');
                $param['s_name'] = $name;
                $param['s_goods_id'] = $goods;
            }else{
                $param['s_name'] = "{$jifen}积分";
            }
            $res = Db::name('sign_prize')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        $goods = Db::name('product')->where(['p_type'=>1,'p_status'=>4,'p_model'=>1])->select ();
        return $this->fetch('sign/sign_add',['goods'=>$goods]);
    }


    public function signEdit(){
        extract (input());
        if(request ()->isPost ()){
            $param = [
                's_day'=>$day,
                's_type'=>$type,
//                's_name'=>$name,
                's_num'=>$jifen,
                's_edit_time'=>date('Y-m-d H:i:s')
            ];
            if($type == 2){
                $name = Db::name('product')->where('p_id',$goods)->value('p_name');
                $param['s_name'] = $name;
                $param['s_goods_id'] = $goods;
            }else{
                $param['s_name'] = "{$jifen}积分";
            }
            $res = Db::name('sign_prize')->where('s_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('sign_prize')->where('s_id',$id)->find();
        $goods = Db::name('product')->where(['p_type'=>1,'p_status'=>4,'p_model'=>1])->select ();
        return $this->fetch ('sign/sgin_edit',['data'=>$data,'goods'=>$goods]);
    }

    public function signDel(){
        extract (input());
        $res = Db::name('sign_prize')->where('s_id',$id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function signState(){
        extract (input());
        $res = Db::name('admin')->where('id',$id)->setField ('sign',$num);
        if(!$res){
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }

}